package com.navi.loaddata.loadService.impl.chamberHst;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.gpEntity.*;
import com.navi.loaddata.core.gpEntity.array.WppAdefectF;
import com.navi.loaddata.core.gpEntity.array.WppAdefectGlassF;
import com.navi.loaddata.core.gpEntity.array.WppAdefectPanelF;
import com.navi.loaddata.core.gpEntity.cf.WppFdefectF;
import com.navi.loaddata.core.gpEntity.cf.WppFdefectGlassF;
import com.navi.loaddata.core.gpEntity.cf.WppFdefectPanelF;
import com.navi.loaddata.core.gpEntity.oc.WppCdefectF;
import com.navi.loaddata.core.gpEntity.oc.WppCdefectGlassF;
import com.navi.loaddata.core.gpEntity.oc.WppCdefectPanelF;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.EtlChamberHstV;
import com.navi.loaddata.core.repository.impl.GreenPlumRepositoryImpl;
import com.navi.loaddata.core.util.JdbcUtil;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.core.util.ProcessUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.navi.loaddata.core.constant.GenericDef.*;
import static java.util.stream.Collectors.groupingBy;

@Service
@Transactional(transactionManager = "secondaryPlatformTransactionManager")
public class LoadChamberHstInfoService {
    private static final Logger logger = LoggerFactory.getLogger(LoadChamberHstInfoService.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    private static final Map<String, String> glassNameMap;

    static {
        glassNameMap = new HashMap<>();
        glassNameMap.put(SHOP_ARY, "sor.wpp_aunit_his");
        glassNameMap.put(SHOP_CF, "sor.wpp_funit_his");
        glassNameMap.put(SHOP_OC, "sor.wpp_cunit_his");
    }

    @Autowired
    private GreenPlumRepositoryImpl dao2;
    private List<GpGlassF> glassList;

    public List<EtlChamberHstV> loadChamberHstInfo(EtlConfDModel etlConfD) throws Exception {
        QueryRunner qr_gp = JdbcUtil.getQueryRunner_gp();
        List<EtlChamberHstV> glassList = new ArrayList<>();
        String sql=" select" +
                " ope_id as stepId ," +
                " glass_id as glassId," +
                " prod_id as productId," +
                " prod_spec_ver as productVersion," +
                " eqpt_id as equipId," +
                " unit_eqpt_id as subEquipId," +
//                " trace_level as TraceLevel," +
                " in_timestamp as glassStartTime," +
                " out_timestamp as glassEndTime," +
                " recipe_id  as pid," +
                " ac_recipe_id as recipeId," +
                " link_key AS linkKey," +
                " carrier_name  as cstId" +
                " from " +glassNameMap.get(etlConfD.getShop())+
                " where 1=1" +
                " and db_timestamp>='"+etlConfD.getRunStartTimestamp()+"'" +
                " and db_timestamp<'"+etlConfD.getRunEndTimestamp()+"'"+
                ProcessUtil.getUnitEqptIdHql(etlConfD.getEqpt());
        try {
            switch (etlConfD.getShop()) {
                case "ARRAY":
                    glassList = qr_gp.query(sql+" and length(glass_id)=10", new BeanListHandler<EtlChamberHstV>(EtlChamberHstV.class));
                    break;
                case "CF":
                    glassList = qr_gp.query(sql+" and length(glass_id)=10", new BeanListHandler<EtlChamberHstV>(EtlChamberHstV.class));
                    break;
                case "OC":
                    if ("CELL1".equals(etlConfD.getCellType())) {
                        glassList = qr_gp.query(sql+" and ope_id<'L4600' and length(glass_id)=10", new BeanListHandler<EtlChamberHstV>(EtlChamberHstV.class));
                        if(glassList.size()>0){
                            glassList.forEach(glass-> {
                                glass.setComponentId(glass.getGlassId());
                                if ("A".equals(glass.getGlassId().substring(0,1))) {
                                    glass.setComponentType("1");
                                }else if("C".equals(glass.getGlassId().substring(0,1))) {
                                    glass.setComponentType("4");
                                }else{
                                    glass.setComponentType("10");
                                }
                            });

                        }
                    } else if ("CELL2".equals(etlConfD.getCellType())) {
                        glassList = qr_gp.query(sql+" and ope_id>='L4600' and length(glass_id)=12", new BeanListHandler<EtlChamberHstV>(EtlChamberHstV.class));
                        if(glassList.size()>0){
                            glassList.forEach(glass-> {
                                glass.setChipId(glass.getGlassId());
                                glass.setChipNo(glass.getGlassId().substring(glass.getGlassId().length()-2,glass.getGlassId().length()));
                            });
                        }
                    } else {
                        throw new ETLException2(ErrorCode.ERROR_CELL_TYPE,null);
                    }
                    break;
                default:
                    throw new ETLException2(ErrorCode.ERROR_SHOP,null);
            }
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_GET_GP_CHAMBER_HST, e);
        }
        List<EtlChamberHstV> glassList2 = new ArrayList<>();
        for (EtlChamberHstV glass : glassList) {
            if (glass.getGlassId().length() < 10) {
                logger.warn("glassId={} stepId={} glassEndTime={} 数据不合规范!跳过!", glass.getGlassId(), glass.getStepId(), glass.getGlassEndTime());
                continue;
            }
            glassList2.add(glass);
        }
        return glassList2;
    }

}
